home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / MAVec.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  5.0 KB  |  201 lines

  1. #include <proto/exec.h>
  2. #include <clib/extras_protos.h>
  3.  
  4.  
  5. /****** extras.lib/MultiAllocVecA ******************************************
  6. *
  7. *   NAME
  8. *       MultiAllocVecA -- Multiple AllocVec.
  9. *       MultiAllocVec -- varargs stub.
  10. *
  11. *   SYNOPSIS
  12. *       succes = MultiAllocVecA(Flags, VecTagList)
  13. *
  14. *       BOOL MultiAllocVec(ULONG, struct VecTag *);
  15. *
  16. *       succes = MultiAllocVecA(Flags, VecTag)
  17. *
  18. *       BOOL MultiAllocVecA(Flags, ULONG, ...);
  19. *
  20. *   FUNCTION
  21. *       Attempt to allocate one or more memory chunks
  22. *       using AllocVec.
  23. *
  24. *   INPUTS
  25. *       Flags - MA_FAILSIZE0: fail all allocations if any
  26. *               have a size of 0.  if your application will be
  27. *               allocating memory of dynamic sizes, and if
  28. *               you want allocations of 0 bytes to fail, then
  29. *               set this flag.
  30. *       VecTag - pointer to an array of struct VecTag.
  31. *                  vt_Ptr is the address of a pointer.
  32. *                  vt_Size is the size of the allocation.
  33. *                  vt_MemFlags are the exec memory (MEMF_) flags. 
  34. *                Last tag should have vt_Ptr = NULL.
  35. *                 
  36. *   RESULT
  37. *       zero if it couldn't allocate the requested memory. or non-zero
  38. *       on success. In either case, vt_Ptrs will be point to a allocated 
  39. *       memory chunk or NULL.  
  40. *
  41. *   EXAMPLE
  42. *       EX1:
  43. *         struct foo *bar;
  44. *         STRPTR dest;
  45. *         APTR cow;
  46. *
  47. *        if( MultiAllocVec(0,
  48. *                           &bar,  sizeof(struct foo),  MEMF_CLEAR,
  49. *                           &dest, strlen(str)+1,       MEMF_PUBLIC,
  50. *                           &cow,  100,                 MEMF_FAST|MEMF_PUBLIC,
  51. *                           0))
  52. *         {
  53. *           ...
  54. *           MultiFreeVec(3,bar,dest,cow);
  55. *         }
  56. *
  57. *       EX2: This will never fail.
  58. *         APTR foo;
  59. *         if(MultiAllocVec(0,
  60. *                           &foo,0,MEMF_CLEAR,
  61. *                           0)
  62. *         {...}
  63. *
  64. *       EX3: This will always fail.
  65. *         APTR foo;
  66. *         if(MultiAllocVec(MA_FAILSIZE0,
  67. *                           &foo,0,MEMF_CLEAR,
  68. *                           0)
  69. *         {...}
  70. *
  71. *   NOTES
  72. *       requires exec.library to be open.
  73. *
  74. *       if the MA_FAILSIZE0 flag is not set, 0 byte allocations
  75. *       will pass even though no memory will be allocated for that.
  76. *       entry and mt_Ptr will be set to 0.
  77. *
  78. *       The memory allocated may be freed individually with 
  79. *       exec.library/FreeVec()
  80. *
  81. *   BUGS
  82. *
  83. *   SEE ALSO
  84. *       MultiFreeVecA(), MultiAllocMemA(),MultiFreeMemA(),
  85. *       MultiAllocPooledA(),MultiFreePooledA(),
  86. *       exec.library/AllocVec(), exec.library/FreeVec()
  87. *       exec.library/AllocMem(), exec.library/FreeMem()
  88. *       exec.library/AllocPooled(), exec.library/FreePooled()
  89. ******************************************************************************
  90. *
  91. */
  92.  
  93.  
  94. BOOL MultiAllocVec(ULONG Flags, ULONG VecTag, ... )
  95. {
  96.   return(MultiAllocVecA(Flags,(struct VecTag *)&VecTag));
  97. }
  98.  
  99. BOOL MultiAllocVecA(ULONG Flags, struct VecTag *VecTagList)
  100. {
  101.   struct VecTag *tag;
  102.   
  103.   if(VecTagList)
  104.   {
  105.     tag=VecTagList;
  106.     while(tag->vt_Ptr)
  107.     {
  108.       *tag->vt_Ptr=0;
  109.       tag++;
  110.     }
  111.  
  112.     tag=VecTagList;
  113.     
  114.     if(Flags & MA_FAILSIZE0)
  115.     {
  116.       while(tag->vt_Ptr)
  117.       {
  118.         if(tag->vt_Size==0) return(FALSE);
  119.         tag++;
  120.       }
  121.       tag=VecTagList;
  122.     }
  123.        
  124.     while(tag->vt_Ptr)
  125.     {
  126.       if(tag->vt_Size)
  127.       {
  128.         if(!(*tag->vt_Ptr=AllocVec(tag->vt_Size,tag->vt_MemFlags)))
  129.         {
  130.           tag=VecTagList;
  131.           while(tag->vt_Ptr)
  132.           {
  133.             if(tag->vt_Ptr) FreeVec(*tag->vt_Ptr);
  134.             tag++;
  135.           }
  136.           return(FALSE);     
  137.         }
  138.       }
  139.       else
  140.         *tag->vt_Ptr=0;
  141.       
  142.       tag++;
  143.     }
  144.     return(TRUE);
  145.   }
  146.   return(FALSE);
  147. }
  148.  
  149. /****** extras.lib/MultiFreeVecA ******************************************
  150. *
  151. *   NAME
  152. *       MultiFreeVecA -- Free multiple memory chunks.
  153. *       MultiFreeVec -- varargs stub.
  154. *
  155. *   SYNOPSIS
  156. *       MultiFreeVecA(Args,MemBlockList)
  157. *
  158. *       MultiFreeVecA(ULONG, APTR *);
  159. *
  160. *       MultiFreeVec(Args, MemBlock)
  161. *
  162. *       void MultiFreeVec(ULONG, ULONG, ... );
  163. *
  164. *   FUNCTION
  165. *       Free multiple memory blocks allocated with MultiAllocVecA()
  166. *       or exec.library/AllocVec(). 
  167. *
  168. *   INPUTS
  169. *       Args - Number of blocks that are to be freed.
  170. *       MemBlockList - An array of pointers to memory
  171. *                      blocks to be freed or NULL.
  172. *
  173. *   RESULT
  174. *       none.
  175. *
  176. *   NOTES
  177. *       requires exec.library to be open.
  178. *
  179. *   SEE ALSO
  180. *       MultiAllocVecA(), MultiFreeVecA(), MultiAllocMemA(), MultiFreeMemA(),
  181. *       MultiAllocPooledA(), MultiFreePooledA(),
  182. *       exec.library/AllocVec(), exec.library/FreeVec()
  183. *       exec.library/AllocMem(), exec.library/FreeMem()
  184. *       exec.library/AllocPooled(), exec.library/FreePooled()
  185. ******************************************************************************
  186. *
  187. */
  188.  
  189. void MultiFreeVec(ULONG Args, APTR MemBlock, ... )
  190. {
  191.   MultiFreeVecA(Args,&MemBlock);
  192. }
  193.  
  194. void MultiFreeVecA(ULONG Args, APTR *MemBlockList)
  195. {
  196.   LONG l;
  197.   
  198.   for(l=0;l<Args;l++)
  199.     FreeVec(MemBlockList[l]);
  200. }
  201.